"""
MVC
"""


class CommodityModel:
    def __init__(self, name="", price=0, sid=0):
        self.name = name
        self.price = price
        self.sid = sid


class CommodityView:
    def __init__(self):
        self.__comm_controller = CommodityController() # 该私有的情况下要私有变量

    def show_comm_message(self):
        print("按1天添加商品信息")
        print("按2天查询商品信息")
        print("按3天删除商品信息")
        print("按4天修改商品信息")

    def select_menu(self):
        num = input("-----请输入数字:")
        if num == "1":
            self.__add_comm_message()
        elif num == "2":
            self.__query_comm_message()
        elif num == "3":
            self.__delete_comm_message()
        else:
            self.__modify_comm_message()

    # 该私有化的方法也私有化
    def __add_comm_message(self):
        model = CommodityModel()
        model.name = input("请输入商品名称")
        model.price = int(input("输入商品价格"))
        # 添加商品
        self.__comm_controller.add_comm(model)

    def __query_comm_message(self):
        # print("------", self.comm_controller.comm_list.__dict__)
        for item in self.__comm_controller.comm_list:
            print(f"商品名称是:{item.name} 价格是:{item.price} 编号是:{item.sid}")

    def __delete_comm_message(self):
        del_sid = int(input("请输入要删除的学生id:"))
        if self.__comm_controller.del_comm(del_sid):
            print("删除成功了")
        else:
            print("删除失败了")

    def __modify_comm_message(self):
        model = CommodityModel()
        model.sid = int(input("请输入要修改商品id编号:"))
        model.name = input("请输入要修改商品名称")
        model.price = input("请输入要修改商品价格")
        if self.__comm_controller.is_update_comm(model):
            print("修改成功了")
        else:
            print("修改失败了")


class CommodityController:
    def __init__(self):
        self.comm_list = []  # type:list[CommodityModel]
        self.comm_model = CommodityModel()
        self.start_sid = 10001  # 要在这里给sid值,不能在model里面赋值

    def add_comm(self, model):
        model.sid = self.start_sid
        self.start_sid += 1
        print("当前sid是", model.sid)
        self.comm_list.append(model)

    def del_comm(self, del_sid):
        for i in range(len(self.comm_list)):
            if self.comm_list[i].sid == del_sid:
                del self.comm_list[i]
                return True
            return False

    def is_update_comm(self, model):
        for item in self.comm_list:
            if item.sid == model.sid:
                item.name = model.name
                item.price = model.price
                return True
        return False


view = CommodityView()

view.show_comm_message()
while True:
    view.select_menu()
